//
//  TabBar.swift
//  SinaBlog
//
//  Created by 有何不可 on 15/12/30.
//  Copyright © 2015年 有何不可. All rights reserved.
//

import UIKit

class TabBar: UITabBar {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setSubViews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setSubViews() {
        
        addSubview(button)
    }
    
    lazy var button: UIButton = {
        
        let button = UIButton(type: .Custom)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        
        return button
        }()
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        button.center = CGPointMake(bounds.width * 0.5, bounds.height * 0.5)
        
        var i = 0
        
        let width = bounds.width * 0.2
        
        for item in subviews {
            
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                item.frame = CGRectMake(CGFloat(i) * width, bounds.origin.y, width, bounds.height)
                ++i
                
                if i == 2 {
                    
                    ++i
                }
            }
        }
    }
    
}
